package com.zzptc.myk.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.zzptc.myk.myapplication.entity.CheliangPinpai;
import com.zzptc.myk.myapplication.entity.Qiche;
import com.zzptc.myk.myapplication.entity.QichePinpai;
import com.zzptc.myk.myapplication.entity.QqCheshi;
import com.zzptc.myk.myapplication.entity.WanliNian;
import com.zzptc.myk.myapplication.service.CheliangPinpaiService;
import com.zzptc.myk.myapplication.service.QichePinpaiService;
import com.zzptc.myk.myapplication.service.QicheService;
import com.zzptc.myk.myapplication.service.QqCheshiService;
import com.zzptc.myk.myapplication.service.WanlinNianService;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getQqCheshiInfo();
       // getQichePinpaiInfo();
        //getWanliNianInfo();
       // getCheliangPinpai();
        //getQicheInfo();
    }

    private void getQqCheshiInfo() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://japi.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        QqCheshiService qqCheshiService = retrofit.create(QqCheshiService.class);
        Map<String,String > map=new HashMap<>();
        map.put("key","025ba3481ed9f7d63072fbde5a199ec1");
        map.put("qq","2452238539");
        Call<QqCheshi> all = qqCheshiService.getAll(map);
        all.enqueue(new Callback<QqCheshi>() {
            @Override
            public void onResponse(Call<QqCheshi> call, Response<QqCheshi> response) {
                QqCheshi body = response.body();
                Log.d(TAG, "onResponse: "+body.getResult().getData().getConclusion());
            }

            @Override
            public void onFailure(Call<QqCheshi> call, Throwable t) {

            }
        });
    }

    private void getQichePinpaiInfo() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        QichePinpaiService qichePinpaiService = retrofit.create(QichePinpaiService.class);
        //Call<QichePinpai> a = qichePinpaiService.getAll("79729168b7661c89f1f4e56936121f31", "A");
        Map<String, String> m = new HashMap<>();
        m.put("key","79729168b7661c89f1f4e56936121f31");
        m.put("first_letter","A");
        Call<QichePinpai> all = qichePinpaiService.getAll(m);
        all.enqueue(new Callback<QichePinpai>() {
            @Override
            public void onResponse(Call<QichePinpai> call, Response<QichePinpai> response) {
                QichePinpai qichePinpai=response.body();
                Log.d(TAG, "onResponse: "+qichePinpai.getResult().get(0).getBrand_name());

            }

            @Override
            public void onFailure(Call<QichePinpai> call, Throwable t) {

            }
        });

    }

    private void getWanliNianInfo() {
        Retrofit r=new Retrofit.Builder()
                .baseUrl("http://v.juhe.cn/calendar/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        WanlinNianService wanlinNianService = r.create(WanlinNianService.class);
        Call<WanliNian> all = wanlinNianService.getAll("2015-1-1","ce74c14d5f06ed664059907889a55eb2");
        all.enqueue(new Callback<WanliNian>() {
            @Override
            public void onResponse(Call<WanliNian> call, Response<WanliNian> response) {
                WanliNian body = response.body();
                Log.d(TAG, "onResponse: "+body.getResult().getData().getHoliday());
            }

            @Override
            public void onFailure(Call<WanliNian> call, Throwable t) {

            }
        });
    }

    private void getCheliangPinpai() {
        Retrofit r=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CheliangPinpaiService cheliangPinpaiService = r.create(CheliangPinpaiService.class);
        Call<CheliangPinpai> a = cheliangPinpaiService.getAll("79729168b7661c89f1f4e56936121f31", "A");
        a.enqueue(new Callback<CheliangPinpai>() {
            @Override
            public void onResponse(Call<CheliangPinpai> call, Response<CheliangPinpai> response) {
                CheliangPinpai ch=response.body();
                Log.d(TAG, "onResponse: "+ch.getResult().get(3).getBrand_name());
            }

            @Override
            public void onFailure(Call<CheliangPinpai> call, Throwable t) {

            }
        });


    }




    private void getQicheInfo() {

        Retrofit r=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        QicheService qicheService = r.create(QicheService.class);
        Call<Qiche> all = qicheService.getAll();
        all.enqueue(new Callback<Qiche>() {
            @Override
            public void onResponse(Call<Qiche> call, Response<Qiche> response) {
                Qiche qichelist = response.body();
                Log.d(TAG, "onResponse: "+new Gson().toJson(qichelist));
            }

            @Override
            public void onFailure(Call<Qiche> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }
}
